/**
 * Copyright (c) 2011-2021, James Zhan 詹波 (jfinal@126.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jfinal.server.undertow;

import io.undertow.server.handlers.resource.ClassPathResourceManager;
import io.undertow.server.handlers.resource.FileResourceManager;
import io.undertow.server.handlers.resource.ResourceManager;

import java.io.File;
import java.util.Collection;
import java.util.LinkedHashSet;

import static com.jfinal.server.undertow.util.ResourceUtils.CLASSPATH_URL_PREFIX;
import static com.jfinal.server.undertow.util.ResourceUtils.FILE_URL_PREFIX;

/**
 * 1：FileResourceManager 支持在普通目录下去加载 css、js 等 web 静态资源
 * <p>
 * 2：ClassPathResourceManager 支持在 classpath 下与 jar 包内加载 css、js
 * 等 web 静态资源
 * <p>
 * 3：ClassPathResourceManager 第二个参数不能省略，也不能为 ""，否则可以在浏览器地址栏直接访问
 * class path、jar 中的所有资源，包括配置文件，下面的用法极其危险:
 * new ClassPathResourceManager(getClassLoader()));
 * <p>
 * 4：在需要用到 ClassPathResourceManager 时为其给定一个参数，例如：
 * new ClassPathResourceManager(getClassLoader(), "webapp"));
 * <p>
 * 上例给定 webapp 参数，undertow 可以从 classpath、jar 包内的
 * webapp 之下去加载 web 静态资源
 */
public class ResourceManagerBuilder {
    private final Collection<String> resourcePathList = new LinkedHashSet<>();

    public void addResourcePath(String resourcePath) {
        if (null != resourcePath && !resourcePath.trim().isEmpty()) {
            resourcePathList.add(resourcePath.trim());
        }
    }

    public ResourceManager build(ClassLoader classLoader) {
        final CompositeResourceManager manager = new CompositeResourceManager();

        resourcePathList.forEach(location -> {
            if (location.startsWith(CLASSPATH_URL_PREFIX)) {
                final String path = location.substring(CLASSPATH_URL_PREFIX.length());

                if (!path.isEmpty()) {
                    manager.add(new ClassPathResourceManager(classLoader, path));
                }
            } else if (location.startsWith(FILE_URL_PREFIX)) {
                final File file = new File(location.substring(FILE_URL_PREFIX.length()));

                if (file.isDirectory()) {
                    manager.add(new FileResourceManager(file));
                }
            } else {
                final File file = new File(location);

                if (file.isDirectory()) {
                    manager.add(new FileResourceManager(file));
                }
            }
        });

        return manager;
    }

}





